package cn.itcast.controller.product;


import cn.itcast.controller.BaseController;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.domain.product.ProductExample;
import cn.itcast.domain.product.Product;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.service.cargo.ProductService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@RequestMapping("/basics/product")
@Controller
public class ProductController extends BaseController {

    @Reference
    private ProductService productService;

    @Reference
    private FactoryService factoryService;
    /**
     * 1.分页查询
     * @param page
     * @param size
     * @return
     */
    @RequestMapping("/list.do")
    public String list(@RequestParam(defaultValue = "1")int page,@RequestParam(defaultValue = "10")int size){
        ProductExample example = new ProductExample();
        PageInfo pageInfo = productService.findPageAll(example, page, size);
        request.setAttribute("page",pageInfo);
        return "/basics/product/pro-list";
    }

    /**
     * 2.进入新增页面
     * /product/toAdd
     * @return
     */
    @RequestMapping("/toAdd.do")
    public String toAdd(){
        FactoryExample example = new FactoryExample();
        List<Factory> factoryList = factoryService.findAll(example);
        request.setAttribute("factoryList",factoryList);
        return "/basics/product/pro-add";
    }


    /**
     *3.新增///修改
     * @param product
     * @return
     */
    @RequestMapping("/edit")
    public String edit(Product product){
        if(StringUtils.isEmpty(product.getId())){
            //-注意需要根据货物的id查询生产厂家--在保存的时候我要得到工厂的名字
            Factory factory = factoryService.findById(product.getFactoryId());
            String factoryName = factory.getFactoryName();
            product.setFactoryName(factoryName);
            productService.save(product);
        }else{
            productService.update(product);
        }
        return "redirect:/basics/product/list.do";
    }

    /**
     * 4.进入修改页面
     * @param id
     * @return
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id){
        //1.查询生产厂家--回显
        FactoryExample example = new FactoryExample();
        List<Factory> factoryList = factoryService.findAll(example);
        request.setAttribute("factoryList",factoryList);
        //2.根据id查询返回商品信息
        Product product = productService.findByID(id);
        request.setAttribute("product",product);
        return "/basics/product/pro-update";
    }

    /**
     * 5.根据id删除
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public String delete(String id){
        productService.delete(id);
        return "redirect:/basics/product/list.do";
    }
}
